// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const Axios = require("axios")

module.exports = function (api) {
    api.loadSource(async ({addCollection}) => {
        // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
        const indexs = addCollection('index')
        const blogs = addCollection('Blog')
        const projects = addCollection('Project')
        const socials = addCollection('Social')
        const socialgzs = addCollection('Socialgz')

        const {data: indexData} = await Axios.get("https://api.github.com/gists/391edc7de20627506f8cee6a756f8a34")
        const {data: blogData} = await Axios.get("https://api.github.com/users/GitHub-Laziji/gists?page=1&per_page=20")
        const {data: projectData} = await Axios.get("https://api.github.com/users/GitHub-Laziji/repos?page=1&per_page=20")
        const {data: socialData} = await Axios.get("https://api.github.com/users/GitHub-Laziji/followers?page=1&per_page=9")
        const {data: socialgzData} = await Axios.get("https://api.github.com/users/GitHub-Laziji/following?page=1&per_page=9")

        for (let key in indexData.files) {
            // 数据正常传回
            const temp = {
                id: "391edc7de20627506f8cee6a756f8a34",
                title: key,
                content: indexData.files[key]['content'],
                description: indexData.description,
                createTime: indexData['created_at'],
                updateTime: indexData['updated_at']
            }
            indexs.addNode(temp)
        }

        for (let key in blogData) {
            const temp = {
                title: Object.keys(blogData[key].files)[0],
                url: blogData[key].comments_url,
                description: blogData[key].description,
                id: blogData[key].files.id,
                createTime: blogData[key].created_at,
                updateTime: blogData[key].updated_at,
            }
            blogs.addNode(temp)
        }

        for (let key in projectData) {
            const temp = {
                id: projectData[key]['id'],
                name: projectData[key]['name'],
                url: projectData[key]['html_url'],
                description: projectData[key]['description'],
                stargazersCount: projectData[key]['stargazers_count'],
                watchersCount: projectData[key]['watchers_count'],
                forksCount: projectData[key]['forks_count'],
                language: projectData[key]['language'],
                license: projectData[key]['license'] ? projectData[key]['license']['spdx_id'] : null,
                createTime: projectData[key]['created_at'],
                updateTime: projectData[key]['updated_at'],
            }
            projects.addNode(temp)
        }

        for (let key in socialData) {
            const temp = {
                name: socialData[key]['login'],
                avatarUrl: socialData[key]['avatar_url'],
                htmlUrl: socialData[key]['html_url'],
            }
            socials.addNode(temp)
        }

        for(let key in socialgzData){
            const temp = {
                name: socialgzData[key]['login'],
                avatarUrl: socialgzData[key]['avatar_url'],
                htmlUrl: socialgzData[key]['html_url'],
            }
            socialgzs.addNode(temp)
        }

    })

    api.createPages(({createPage}) => {
        // Use the Pages API here: https://gridsome.org/docs/pages-api/
    })
}
